﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;

namespace GTA
{
    public partial class FrmSplash : Form
    {
        public FrmSplash()
        {
            InitializeComponent();
        }
        private void frmSplash_Load(object sender, EventArgs e)
        {
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
            lblAssemblyCompany.Text = attributes.Length > 0 ? ((AssemblyCompanyAttribute)attributes[0]).Company : String.Empty;
            attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
            lblAssemblyCopyright.Text = attributes.Length > 0 ? ((AssemblyCopyrightAttribute)attributes[0]).Copyright : String.Empty;
            lblAssemblyVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            Thread trdStart = new Thread(new ThreadStart(StartForm));
            trdStart.Start();
        }
        private void StartForm()
        {
            try
            {
                SetDescription("Inicializando Sistema.");
                Thread.Sleep(1000);
                SetDescription("Carregando configurações do sistema.");
                Program.LoadSystemConfiguration();
                SetDescription("Inicializando comunicação com TeamSystem.");
                Program.StartTeamSystemInstance();
                SetDescription("Carregando Lista de Projetos.");
                Program.LoadProjects();
                SetDescription("Atualizando Lista de Projetos do Usuário");
                Program.UpdateLoggedUserProjects();
                this.Tag = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ocorreu um erro durante a inicialização.\r\nDetalhes: " + GTA.Common.Util.GetFullException(ex), "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Tag = false;
            }
            finally
            {
                CloseForm();
            }
        }
        private delegate void CloseFormDelegate();
        private void CloseForm()
        {
            if (this.InvokeRequired)
                this.Invoke(new CloseFormDelegate(CloseForm));
            else
                this.Close();
        }
        private delegate void DefineDescriptionDelegate(String description);
        private void SetDescription(String description)
        {
            if (lbDescription.InvokeRequired)
                lbDescription.Invoke(new DefineDescriptionDelegate(SetDescription), new object[] { description });
            else
                lbDescription.Text = description;
        }
    }
}
